/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.acheieve.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.goodsinfo.entity.Goodsinfo;
import com.jeesite.modules.goodsinfo.service.GoodsinfoService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.acheieve.entity.Acheieve;
import com.jeesite.modules.acheieve.service.AcheieveService;

/**
 * js_acheieveController
 * @author 达芬奇
 * @version 2019-10-05
 */
@Controller
@RequestMapping(value = "${adminPath}/acheieve/acheieve")
public class AcheieveController extends BaseController {

	@Autowired
	private AcheieveService acheieveService;
	@Autowired
	private GoodsinfoService goodsinfoService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Acheieve get(String id, boolean isNewRecord) {
		return acheieveService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("acheieve:acheieve:view")
	@RequestMapping(value = {"list", ""})
	public String list(Acheieve acheieve, Model model) {
		model.addAttribute("acheieve", acheieve);
		return "modules/acheieve/acheieveList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("acheieve:acheieve:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Acheieve> listData(Acheieve acheieve, HttpServletRequest request, HttpServletResponse response) {
		acheieve.setPage(new Page<>(request, response));
		Page<Acheieve> page = acheieveService.findPage(acheieve);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("acheieve:acheieve:view")
	@RequestMapping(value = "form")
	public String form(Acheieve acheieve, Model model) {
		model.addAttribute("acheieve", acheieve);
		return "modules/acheieve/acheieveForm";
	}

	/**
	 * 保存个人农作物产出仓库
	 */
	@RequiresPermissions("acheieve:acheieve:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Acheieve acheieve) {
		acheieveService.save(acheieve);
		return renderResult(Global.TRUE, text("保存个人农作物产出仓库成功！"));
	}
	
	/**
	 * 删除个人农作物产出仓库
	 */
	@RequiresPermissions("acheieve:acheieve:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Acheieve acheieve) {
		acheieveService.delete(acheieve);
		return renderResult(Global.TRUE, text("删除个人农作物产出仓库成功！"));
	}


	/**
	 * 上架个人农作物产出仓库
	 */
	@RequiresPermissions("acheieve:acheieve:edit")
	@RequestMapping(value = "display")
	@ResponseBody
	public String display(Acheieve acheieve) {
		acheieve.setState(1);
		Goodsinfo goodsinfo = new Goodsinfo();
		goodsinfo.setAddress("达芬奇农场");
		goodsinfo.setGoodsdes(acheieve.getCropname());
		goodsinfo.setGoodsnum(acheieve.getAchievenum());
		goodsinfo.setUid(UserUtils.getUser().getId());
		goodsinfo.setGoodsid(acheieve.getId());
		goodsinfoService.save(goodsinfo);
		acheieveService.save(acheieve);
		return renderResult(Global.TRUE, text("上架此农作物成功！"));
	}
	/**
	 * 下架个人农作物产出仓库
	 */
	@RequiresPermissions("acheieve:acheieve:edit")
	@RequestMapping(value = "undisplay")
	@ResponseBody
	public String undisplay(Acheieve acheieve) {
		acheieve.setState(0);
		Goodsinfo goodsinfo = new Goodsinfo();
		goodsinfo.setAddress("达芬奇农场");
		goodsinfo.setGoodsdes(acheieve.getCropname());
		goodsinfo.setGoodsnum(acheieve.getAchievenum());
		goodsinfo.setUid(UserUtils.getUser().getId());
		goodsinfo.setGoodsid(acheieve.getId());
		Goodsinfo goodsinfo1 =goodsinfoService.findList(goodsinfo).get(0);
		goodsinfoService.delete(goodsinfo1);
		acheieveService.save(acheieve);
		return renderResult(Global.TRUE, text("下架个此农作物成功！"));
	}
}